package com.qxsx.midi.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Zrj on 2015/9/1.
 */
public class SetPassWordAcitvity extends BaseActivity implements View.OnClickListener {
    private CustomProgress customProgress;

    private Button btn_sure;
    private EditText et_password, et_password_again;
    Intent intent = new Intent();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        setTitle("设置密码");
        setNavBtn(R.mipmap.back, 0);
        findById();
    }

    private void findById() {
        btn_sure = (Button) findViewById(R.id.btn_sure);
        btn_sure.setOnClickListener(this);
        et_password = (EditText) findViewById(R.id.et_password);
        et_password_again = (EditText) findViewById(R.id.et_password_again);

    }


    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_sure:
                if (!et_password_again.getText().toString().equals(et_password.getText().toString())) {
                    ShowToast("两次请输入相同的密码");
                } else {
                    intent = getIntent();
                    String phone = intent.getStringExtra("phone");
                    customProgress = CustomProgress.show(SetPassWordAcitvity.this, "请稍后....", true, null);

                    JSONObject object = new JSONObject();
                    JSONArray array = new JSONArray();

                    try {
                        object.put("phone", phone);
                        object.put("pwd", et_password.getText().toString());
                        array.put(object);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    MyNet.doPostJsonArray("108", array, new callBackJsonArray() {
                        @Override
                        public void run(Object code, Object message, JSONArray array) {
                            if (array == null) {
                                Log.w("array", array + "");
                                return;
                            }
                            if (code.toString().equals("200")) {
                                ShowToast(message.toString());
                                intent.setClass(AppContext.getContext(), LoginActivity.class);
                                startActivity(intent);
                                finish();
                            }
                            customProgress.dismiss();
                        }
                    });
                }
                break;
            default:
                break;
        }
    }
}
